package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.JyjcImportSebs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.JyjcImportSpsrMapper;
import com.ruoyi.system.domain.JyjcImportSpsr;
import com.ruoyi.system.service.IJyjcImportSpsrService;

/**
 * 税票收入Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-19
 */
@Service
public class JyjcImportSpsrServiceImpl implements IJyjcImportSpsrService 
{
    private static final Logger log = LoggerFactory.getLogger(JyjcImportSpsrServiceImpl.class);
    @Autowired
    private JyjcImportSpsrMapper jyjcImportSpsrMapper;

    /**
     * 查询税票收入
     * 
     * @param id 税票收入主键
     * @return 税票收入
     */
    @Override
    public JyjcImportSpsr selectJyjcImportSpsrById(Long id)
    {
        return jyjcImportSpsrMapper.selectJyjcImportSpsrById(id);
    }

    /**
     * 查询税票收入列表
     * 
     * @param jyjcImportSpsr 税票收入
     * @return 税票收入
     */
    @Override
    public List<JyjcImportSpsr> selectJyjcImportSpsrList(JyjcImportSpsr jyjcImportSpsr)
    {
        return jyjcImportSpsrMapper.selectJyjcImportSpsrList(jyjcImportSpsr);
    }

    /**
     * 新增税票收入
     * 
     * @param jyjcImportSpsr 税票收入
     * @return 结果
     */
    @Override
    public int insertJyjcImportSpsr(JyjcImportSpsr jyjcImportSpsr)
    {
        return jyjcImportSpsrMapper.insertJyjcImportSpsr(jyjcImportSpsr);
    }

    /**
     * 修改税票收入
     * 
     * @param jyjcImportSpsr 税票收入
     * @return 结果
     */
    @Override
    public int updateJyjcImportSpsr(JyjcImportSpsr jyjcImportSpsr)
    {
        return jyjcImportSpsrMapper.updateJyjcImportSpsr(jyjcImportSpsr);
    }

    /**
     * 批量删除税票收入
     * 
     * @param ids 需要删除的税票收入主键
     * @return 结果
     */
    @Override
    public int deleteJyjcImportSpsrByIds(Long[] ids)
    {
        return jyjcImportSpsrMapper.deleteJyjcImportSpsrByIds(ids);
    }

    /**
     * 删除税票收入信息
     * 
     * @param id 税票收入主键
     * @return 结果
     */
    @Override
    public int deleteJyjcImportSpsrById(Long id)
    {
        return jyjcImportSpsrMapper.deleteJyjcImportSpsrById(id);
    }

    @Override
    public String importJyjcImportSpsr(List<JyjcImportSpsr> jyjcImportSpsrList, String yearyf) {
        String [] sj=yearyf.split("-");

        if (StringUtils.isNull(jyjcImportSpsrList) || jyjcImportSpsrList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        jyjcImportSpsrMapper.deleteJyjcImportSpsrByYearYf(sj[0]+"-"+sj[1]);
        for (JyjcImportSpsr jyjcImportSpsr : jyjcImportSpsrList)
        {
            try
            {
                jyjcImportSpsr.setYear(sj[0]);
                jyjcImportSpsr.setYf(sj[1]);
                jyjcImportSpsr.setYearyf(sj[0]+"-"+sj[1]);
                jyjcImportSpsrMapper.insertJyjcImportSpsr(jyjcImportSpsr);
                successNum++;
                successMsg.append("<br/>" + successNum + "、 " + sj[0]+"年"+sj[1]+"月"+jyjcImportSpsr.getDeptname()  + " 导入成功");

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + sj[0]+"年"+sj[1]+"月"+jyjcImportSpsr.getDeptname()   + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
